﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Windows.Forms;

namespace HTF_HMI
{
    public partial class AUDIT_TASK_EDIT : Form
    {      
        public bool isAdd = true;
        public string reportId = string.Empty;        
        private AUDIT_TASK audit_line;

        public AUDIT_TASK_EDIT(AUDIT_TASK act_form,string id)
        {
            InitializeComponent();
            audit_line = act_form;
            BindArea();
            if (!string.IsNullOrEmpty(id))
            {
                reportId = id;
                isAdd = false;
                BindReport(reportId);              
            }
        }

        private void BindArea()
        {
            var area = DBClass.DBHelper.GetAuditArea();
            cmb_area.DisplayMember = "AREA_NAME";
            cmb_area.ValueMember = "AREA_ID";
            cmb_area.DataSource = area;

            var station = DBClass.DBHelper.GetAuditStation();
            cmb_station.DisplayMember = "STATION_NAME";
            cmb_station.ValueMember = "STATION_NAME";
            cmb_station.DataSource = station;
        }

        private void BindReport(string id)
        {   
            var task = DBClass.DBHelper.GetAuditTaskById(id).FirstOrDefault();
            if (task!=null)
            {
                txt_code.Text = task.TASK_CODE;
                txt_name.Text = task.TASK_NAME;                
                txt_id.Text = task.TASK_ID.ToString();
                cmb_area.Text = task.AREA_NAME;
                cmb_station.Text = task.STATION_NAME;
            }
        } 

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void btn_save_Click(object sender, EventArgs e)
        {            
            string strCode = txt_code.Text.Trim();
            string strName = txt_name.Text.Trim();
            string strArea = cmb_area.Text.Trim();
            string strStation = cmb_station.Text.Trim();

            if (string.IsNullOrEmpty(strArea))
            {
                MessageBox.Show("请选择区域！");
                return;
            }
            if (string.IsNullOrEmpty(strStation))
            {
                MessageBox.Show("请选择岗位！");
                return;
            }
            if (string.IsNullOrEmpty(strCode))
            {
                MessageBox.Show("请输入巡检任务编码！");
                return;
            }
            if (string.IsNullOrEmpty(strName))
            {
                MessageBox.Show("请输入巡检任务名称！");
                return;
            }          
          
            try
            {
                string sql = string.Empty;              
                DBEntity.Models.AUDIT_TASK task = new DBEntity.Models.AUDIT_TASK();
                task.TASK_NAME = strName;
                task.AREA_NAME = strArea;
                task.STATION_NAME = strStation;
                task.TASK_CODE = strCode;
                if (!isAdd)
                {   
                    task.TASK_ID =Int64.Parse( txt_id.Text);                                   
                   
                    bool result = DBClass.DBHelper.UpdateAuditTask(task);
                    if (result )
                    {    
                        MessageBox.Show("保存成功！");
                        this.DialogResult = DialogResult.OK;

                        audit_line.btn_query_Click(null, null);
                    }
                }
                else
                {                                                   
                    long id = DBClass.DBHelper.InsertAuditTask(task);
                    if (id > 0)
                    {     
                        MessageBox.Show("保存成功！");
                        this.DialogResult = DialogResult.OK;

                        audit_line.btn_query_Click(null, null);
                    }                            
                }               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                DBClass.Log.InsertLog("保存巡检任务失败！"+ex.Message);  
            }
        }
    }
}
